//QQ互联接口
var Config = require("../config");
var KOC = require("koc.common.utils");

var _Interface_Weixin = module.exports = {
    _RedirectURI: Config.Common.Virtual_Path.Main + "oauth/weixin.token",
    /////////////////////////////////////////
    //取得登录地址_Interface_Weixin._RedirectURI
    /////////////////////////////////////////
    OAuth_LoginUrl: function () {
        return "https://open.weixin.qq.com/connect/qrconnect?" +
            "appid=" + Config.Interface.Weixin.APPID +
            "&redirect_uri=" + encodeURIComponent(_Interface_Weixin._RedirectURI) +
            "&response_type=code" +
            "&scope=snsapi_login" +
            "&state=1#wechat_redirect";
    },
    /////////////////////////////////////////
    //获取AccessToken
    /////////////////////////////////////////
    OAuth_Token: function (code, callback) {
        if (typeof callback !== "function") {
            return;
        }
        var options = {
            uri: "https://api.weixin.qq.com/sns/oauth2/access_token",
            method: "GET",
            qs: {
                grant_type: "authorization_code",
                appid: Config.Interface.Weixin.APPID,
                secret: Config.Interface.Weixin.APPKey,
                code: code
            }
        };
        KOC.Request(options, function (error, response, result) {
            if (error) {
                callback(KOC.ReturnValue.New({
                    hasError: true,
                    message: error.message
                }));
                return;
            }
            result = JSON.parse(result);
            if (!result.access_token) {
                callback(KOC.ReturnValue.New({
                    hasError: true,
                    message: result.errmsg + result.errcode
                }));
                return;
            }
            callback(KOC.ReturnValue.New({
                message: "",
                returnObject: {
                    AccessToken: result.access_token,
                    ExpiresIn: result.expires_in,
                    RefreshToken: result.refresh_token,
                    OpenID: result.openid
                }
            }));
        });
    },
    /////////////////////////////////////////
    //获取OpenID
    /////////////////////////////////////////
    OAuth_OpenID: function (AccessToken, OpenID, callback) {
        if (typeof callback !== "function") {
            return;
        }
        var options = {
            uri: "https://api.weixin.qq.com/sns/auth",
            method: "GET",
            qs: {
                access_token: AccessToken,
                openid: OpenID
            }
        };
        KOC.Request(options, function (error, response, result) {
            if (error) {
                callback(KOC.ReturnValue.New({
                    hasError: true,
                    message: error.message
                }));
                return;
            }
            if (!result) {
                callback(KOC.ReturnValue.New({
                    hasError: true,
                    message: result.errmsg + result.errcode
                }));
                return;
            }
            callback(KOC.ReturnValue.New({
                message: "",
                returnObject: {
                    Status: result.errmsg
                }
            }));
        });
    },
    /////////////////////////////////////////
    //获取用户信息
    /////////////////////////////////////////
    Get_User_Info: function (token, openid, callback) {
        var options = {
            uri: "https://api.weixin.qq.com/sns/userinfo",
            method: "GET",
            qs: {
                access_token: token,
                openid: openid
            }
        };
        KOC.Request(options, function (error, response, result) {
            if (error) {
                callback(KOC.ReturnValue.New({
                    hasError: true,
                    message: error.message
                }));
                return;
            }
            result = JSON.parse(result);
            if (result.errcode) {
                callback(KOC.ReturnValue.New({
                    hasError: true,
                    errorCode: result.errcode,
                    message: result.errmsg
                }));
                return;
            }
            callback(KOC.ReturnValue.New({
                message: "",
                returnObject: result
            }));
        });
    }
};